package com.trs.util;

/**
 *
 * 日期：2023年8月5日下午8:42:38
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public class Results<T> {

	private String code;
	private String msg;
	private boolean isSuccess;
	private T data;

	private Results() {
		super();
	}

	public enum STATUS_CODE {
		OK("200"), Accepted("202"), UnLgoin("401"), Unauthorized("403"), NOT_FOUND("404"), SERVER_ERROR("500");

		private final String value;

		STATUS_CODE(String value) {
			this.value = value;
		}

		public String getValue() {
			return value;
		}
	}

	/**
	 * 构建一个成功的RestfulResults。code为200
	 * 
	 * @param <T>
	 * @return 初始化的RestfulResults
	 */
	public static <T> Results ok() {
		return ok(null);
	}

	/**
	 * 构建一个成功的RestfulResults。code为200
	 * 
	 * @param datas
	 *            结果集
	 * @param <T>
	 * @return 初始化的RestfulResults
	 */

	public static <T> Results ok(T data) {
		Results result = new Results();
		result.code = Results.STATUS_CODE.OK.getValue();
		result.data = data;
		result.isSuccess = true;
		result.msg = "操作成功";
		return result;
	}

	/**
	 * 构建构建一个错误的error。code默认为500
	 * 
	 * @param msg
	 *            错误信息
	 * @param <T>
	 * @return 初始化的RestfulResults
	 */
	public static <T> Results error(String msg, T data) {
		return error(Results.STATUS_CODE.SERVER_ERROR, msg, data);
	}

	/**
	 * 构建构建一个错误的error。code默认为500
	 * 
	 * @param msg
	 *            错误信息
	 * @param <T>
	 * @return 初始化的RestfulResults
	 */
	public static <T> Results error(String msg) {
		return error(Results.STATUS_CODE.SERVER_ERROR, msg, null);
	}

	/**
	 * 构建一个错误的error
	 * 
	 * @param status_code
	 *            错误码
	 * @param msg
	 *            错误信息
	 * @param <T>
	 * @return 初始化的RestfulResults
	 */
	public static <T> Results error(Results.STATUS_CODE status_code, String msg, T data) {
		Results result = new Results();
		result.code = (status_code == null) ? Results.STATUS_CODE.SERVER_ERROR.getValue() : status_code.getValue();
		result.isSuccess = false;
		result.msg = msg;
		result.data = data;
		return result;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public boolean getIsSuccess() {
		return isSuccess;
	}

	public void setIsSuccess(boolean isSuccess) {
		this.isSuccess = isSuccess;
	}

	@Override
	public String toString() {
		return "Results [code=" + code + ", msg=" + msg + ", isSuccess=" + isSuccess + ", data=" + data + "]";
	}

}
